home *** CD-ROM | disk | FTP | other *** search
/ Aminet 40 / Aminet 40 (2000)(Schatztruhe)[!][Dec 2000].iso / Aminet / dev / lang / Python16_Src.lha / Python16_Source / Parser / grammar1.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-08-03  |  1016 b   |  60 lines

  1. /* Grammar subroutines needed by parser */
  2.  
  3. #include "pgenheaders.h"
  4. #include "assert.h"
  5. #include "grammar.h"
  6. #include "token.h"
  7.  
  8. /* Return the DFA for the given type */
  9.  
  10. dfa *
  11. PyGrammar_FindDFA(g, type)
  12.     grammar *g;
  13.     register int type;
  14. {
  15.     register dfa *d;
  16. #if 1
  17.     /* Massive speed-up */
  18.     d = &g->g_dfa[type - NT_OFFSET];
  19.     assert(d->d_type == type);
  20.     return d;
  21. #else
  22.     /* Old, slow version */
  23.     register int i;
  24.     
  25.     for (i = g->g_ndfas, d = g->g_dfa; --i >= 0; d++) {
  26.         if (d->d_type == type)
  27.             return d;
  28.     }
  29.     assert(0);
  30.     /* NOTREACHED */
  31. #endif
  32. }
  33.  
  34. char *
  35. PyGrammar_LabelRepr(lb)
  36.     label *lb;
  37. {
  38.     static char buf[100];
  39.     
  40.     if (lb->lb_type == ENDMARKER)
  41.         return "EMPTY";
  42.     else if (ISNONTERMINAL(lb->lb_type)) {
  43.         if (lb->lb_str == NULL) {
  44.             sprintf(buf, "NT%d", lb->lb_type);
  45.             return buf;
  46.         }
  47.         else
  48.             return lb->lb_str;
  49.     }
  50.     else {
  51.         if (lb->lb_str == NULL)
  52.             return _PyParser_TokenNames[lb->lb_type];
  53.         else {
  54.             sprintf(buf, "%.32s(%.32s)",
  55.                 _PyParser_TokenNames[lb->lb_type], lb->lb_str);
  56.             return buf;
  57.         }
  58.     }
  59. }
  60.